<?php namespace app\model\model;

use think\Model;
use think\Db;

/**
 * 模型层
 */
class User extends Model {

	protected $table = 'gd_user';

	/**
	 * 添加单条数据
	 */
	public function addData($data) {
		return !is_array($data) ? FALSE : Db::table($this -> table) -> insertGetId($data);
	}
	
	/**
	 * 添加多条数据
	 */
	public function addAll($data) {
		return !is_array($data) ? FALSE : Db::table($this -> table) -> insertAll($data);
	}
	
	/**
	 * 删除数据
	 */
	public function delData($where) {
		return (!is_array($where) || empty($where)) ? FALSE : Db::table($this -> table) -> where($where) -> delete();
	}

	/**
	 * 编辑数据
	 */
	public function editData($where, $save) {
		return (!is_array($where) || !is_array($save)) ? FALSE : Db::table($this -> table) -> where($where) -> update($save);
	}

	/**
	 * 获取单条数据
	 */
	public function getSingleInfo($where, $field = '', $orderBy = ''){
		return !is_array($where) ? FALSE : Db::table($this -> table) -> field($field) -> where($where) -> order($orderBy) -> find();
	}

	/**
	 * 获取多条数据
	 */
	public function getMuchInfo($where = '', $field = '', $orderBy = '', $groupBy = '', $page = 0, $num = 0, $whereExp = ''){
		$start = $page > 0 ? (($page - 1) * $num) : '' ;
		return Db::table($this -> table) -> field($field) -> where($where) -> where($whereExp) -> group($groupBy) -> order($orderBy) -> limit($start, $num) -> select();
	}

	/**
	 * 获取条数
	 */
	public function getCountNums($where = '', $groupBy, $whereExp = '') {
		return empty($groupBy) ? Db::table($this -> table) -> where($where) -> where($whereExp) -> count() : Db::table($this -> table) -> where($where) -> where($whereExp) -> group($groupBy) -> count();
	}
	
	/**
	 * 获取某条数据(锁表)
	 */
	public function getDataLock($where){
		return (!$where || !is_array($where)) ? FALSE : Db::table($this -> table) -> where($where) -> lock(true) -> find();
	}

	/**
	 * 获取条数sql语句
	 */
	public function getCountNumsSql($where) {
		return !is_array($where) ? FALSE : Db::table($this -> table) -> where($where)-> buildSql();
	}

	/**
	 *  字段自增
	 * @param string $field 要自增的字段值
	 * @param array $where 条件
	 * @param int $num 要增加的数量
	 */
	public function fieldIncrement($where, $field, $num = 1){
		return (is_null($field) || empty($field) || !is_string($field) || is_null($where) || empty($where) || !is_array($where)) 
				? FALSE 
				: Db::table($this -> table) -> where($where) -> setInc($field, $num);
	}
	
	/**
	 *  字段自减
	 * @param string $field 要自减的字段值
	 */
	public function fieldDecrement($where, $field, $num = 1){
		return (is_null($field) || empty($field) || !is_string($field) || is_null($where) || empty($where) || !is_array($where)) 
				? FALSE 
				: Db::table($this -> table) -> where($where) -> setDec($field, $num);
	}
	
	/**
	 * 关联med_admin_report_type获取多条数据()
	 */
//	public function getJoinAdminReportTypeMuchInfo($where, $field = false, $orderBy = '', $groupBy = '', $page = 0, $num = 0, $join = 'inner', $whereExp = ''){
//		$start = $page > 0 ? (($page - 1) * $num) : '' ;
//		return empty($where) && empty($whereExp) 
//				? FALSE
//				: Db::table($this -> table)
//				-> alias('a')
//				-> join('med_admin_report_type b', 'a.id = b.admin_id', $join)
//				-> field($field)
//              -> where($where)
//              -> where($whereExp)
//				-> group($groupBy) 
//				-> order($orderBy) 
//				-> limit($start, $num) 
//				-> select();
//	}
	
//	public function getJoinAdminReportTypeCount($where, $groupBy = '', $whereExp = '', $join = 'inner'){
//		return empty($where) && empty($whereExp) 
//				? FALSE
//				: Db::table($this -> table)
//				-> alias('a')
//				-> join('med_admin_report_type b', 'a.id = b.admin_id', $join)
//              -> where($where)
//              -> where($whereExp)
//				-> group($groupBy) 
//				-> count();
//	}

}
?>